import request from '@/utils/request'

// 教案接口类型定义
export interface LessonPlan {
  _id: string;
  title: string;
  content: string;
  subject: string;
  grade: string;
  courseType: string;
  tags: string[];
  author: {
    _id: string;
    name: string;
    email: string;
    avatar?: string;
  };
  version: number;
  status: 'draft' | 'published' | 'archived';
  attachments: string[];
  views: number;
  likes: number;
  likedBy?: string[];
  isLiked?: boolean;
  comments: string[];
  isPublic: boolean;
  createdAt: string;
  updatedAt: string;
}

export interface CreateLessonPlanData {
  title: string;
  content: string;
  subject: string;
  grade: string;
  courseType: string;
  tags?: string[];
  attachments?: string[];
  isPublic?: boolean;
  status?: 'draft' | 'published' | 'archived';
}

export interface UpdateLessonPlanData {
  title?: string;
  content?: string;
  subject?: string;
  grade?: string;
  courseType?: string;
  tags?: string[];
  attachments?: string[];
  isPublic?: boolean;
  status?: 'draft' | 'published' | 'archived';
}

export interface LessonPlanListParams {
  page?: number;
  limit?: number;
  subject?: string;
  grade?: string;
  courseType?: string;
  status?: string;
}

export interface SearchParams {
  q: string;
  page?: number;
  limit?: number;
  subject?: string;
  grade?: string;
  courseType?: string;
}

export interface ApiResponse<T> {
  success: boolean;
  message?: string;
  data: T;
  code?: string;
}

export interface PaginationData<T> {
  lessonPlans: T[];
  pagination: {
    current: number;
    limit: number;
    total: number;
    pages: number;
  };
  searchQuery?: string;
}


// 教案API接口
export const lessonPlanApi = {
  // 获取教案列表
  getList: (params?: LessonPlanListParams): Promise<ApiResponse<PaginationData<LessonPlan>>> => {
    return request.get('/lessonplans', { params });
  },

  // 获取我的教案列表
  getMyList: (params?: LessonPlanListParams): Promise<ApiResponse<PaginationData<LessonPlan>>> => {
    return request.get('/lessonplans/my', { params });
  },

  // 获取单个教案
  getById: (id: string): Promise<ApiResponse<LessonPlan>> => {
    return request.get(`/lessonplans/${id}`);
  },

  // 创建教案
  create: (data: CreateLessonPlanData): Promise<ApiResponse<LessonPlan>> => {
    return request.post('/lessonplans', data);
  },

  // 保存草稿
  saveDraft: (data: Partial<CreateLessonPlanData>): Promise<ApiResponse<LessonPlan>> => {
    return request.post('/lessonplans/draft', data);
  },

  // 更新草稿
  updateDraft: (id: string, data: Partial<UpdateLessonPlanData>): Promise<ApiResponse<LessonPlan>> => {
    return request.put(`/lessonplans/${id}/draft`, data);
  },

  // 发布教案
  publish: (id: string): Promise<ApiResponse<LessonPlan>> => {
    return request.put(`/lessonplans/${id}/publish`);
  },

  // 归档教案
  archive: (id: string): Promise<ApiResponse<LessonPlan>> => {
    return request.put(`/lessonplans/${id}/archive`);
  },

  // 更新教案
  update: (id: string, data: UpdateLessonPlanData): Promise<ApiResponse<LessonPlan>> => {
    return request.put(`/lessonplans/${id}`, data);
  },

  // 删除教案
  delete: (id: string): Promise<ApiResponse<void>> => {
    return request.delete(`/lessonplans/${id}`);
  },

  // 搜索教案
  search: (params: SearchParams): Promise<ApiResponse<PaginationData<LessonPlan>>> => {
    return request.get('/lessonplans/search', { params });
  },

  // 获取版本历史
  getVersionHistory: (id: string, params?: { page?: number; limit?: number }): Promise<ApiResponse<any>> => {
    return request.get(`/lessonplans/${id}/versions`, { params });
  },

  // 获取特定版本
  getSpecificVersion: (id: string, version: number): Promise<ApiResponse<any>> => {
    return request.get(`/lessonplans/${id}/versions/${version}`);
  },

  // 恢复到指定版本
  restoreVersion: (id: string, version: number): Promise<ApiResponse<LessonPlan>> => {
    return request.put(`/lessonplans/${id}/versions/${version}/restore`);
  },

  // 切换点赞状态
  toggleLike: (id: string): Promise<ApiResponse<{ liked: boolean; likes: number }>> => {
    return request.post(`/lessonplans/${id}/toggle-like`);
  }
};

export default lessonPlanApi;